package cc.mrbird.febs.system.service.impl;


import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.system.entity.DrawlingMark;
import cc.mrbird.febs.system.entity.Mark;
import cc.mrbird.febs.system.mapper.DrawlingMarkMapper;
import cc.mrbird.febs.system.mapper.MarkMapper;
import cc.mrbird.febs.system.service.IDrawlingMarkService;
import cc.mrbird.febs.system.service.IMarkService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 *  Service实现
 *
 * @author MrBird
 * @date 2019-07-20 23:15:45
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MarkServiceImpl extends ServiceImpl<MarkMapper, Mark> implements IMarkService {

    @Resource
    private MarkMapper markMapper;
    @Resource
    private IDrawlingMarkService drawlingMarkService;

    @Override
    public IPage<Mark> findMarks(QueryRequest request, Mark mark) {
        LambdaQueryWrapper<Mark> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        Page<Mark> page = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(page, queryWrapper);
    }

    @Override
    public List<Mark> findMarks(Mark mark) {
	    LambdaQueryWrapper<Mark> queryWrapper = new LambdaQueryWrapper<>();
        List<Mark> list= this.baseMapper.selectList(queryWrapper);
		// TODO 设置查询条件
		return list;
    }

    @Override
    public List<Mark> findMarks(Integer id) {
        LambdaQueryWrapper<Mark> queryWrapper = new LambdaQueryWrapper<>();
        List<Mark> result= this.baseMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(result)){
            if(null!=id){
                DrawlingMark drawlingMark=new DrawlingMark();
                drawlingMark.setDrawingId(id);
                //获取标记项
                List<DrawlingMark>  list=drawlingMarkService.findDrawlingMarks(drawlingMark);
                if(CollectionUtils.isNotEmpty(list)){
                    result.forEach(mark->{
                       Integer markId= mark.getId();
                        list.forEach(dm->{
                            if(markId==dm.getMarkId()){
                                //全部标记中被选中项
                                mark.setSelected(true);
                            }
                        });
                    });

                }
            }
        }


        return result;
    }

    @Override
    @Transactional
    public void createMark(Mark mark) {
        this.save(mark);
    }

    @Override
    @Transactional
    public void updateMark(Mark mark) {
        this.saveOrUpdate(mark);
    }

    @Override
    @Transactional
    public void deleteMark(Mark mark) {
        LambdaQueryWrapper<Mark> wapper = new LambdaQueryWrapper<>();
	    // TODO 设置删除条件
	    this.remove(wapper);
	}

	public boolean setDrawingMark(Integer drawingId,String []markIds){

        return true;

    }
}
